package com.wooboo.server.ext.action.ad;

import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Logger;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.AdMonitor;
import com.wooboo.server.ext.util.DateUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;
import com.wooboo.server.ext.util.controller.MonitorUtil;
/**
 * 
 * 流量监控
 * 	计划总预算
 * 	组天预算
 * 		注意：第二天凌晨的清空天的中间累计值
 * @author wangff
 *
 */
public class Monitor extends ActionBase{

	public Logger logger = Logger.getLogger(Monitor.class);
	private JSONArray array;
	@Override
	public void initParameters(Parameters params) throws ApiError {
		// 参数验证
		String json = StringUtil.decode(params.getFieldValue("json"), "utf-8");
		if(StringUtil.isEmpty(json)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
		try {
			logger.logInfo("json str="+json);
			array = JSONArray.parseArray(json);
		} catch (Exception e) {
			throw ApiError.Type.INVALID_PARAM.toException("参数不正确") ;
		}
		
		if(array.size()==0){
			throw ApiError.Type.INVALID_PARAM.toException("array.size 为 0 ") ;
		}
	}

	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		String date = DateUtil.getFormatDate(new Date(), "yyyyMMdd");
		
		for(int i=0;i<array.size();i++){
			JSONObject o = array.getJSONObject(i);
			AdMonitor m = JSONObject.toJavaObject(o, AdMonitor.class);
			MonitorUtil.checkIdeaId(m,date);
		}
		
		//判断组
		MonitorUtil.merge2GroupId(date);
		//判断计划
		MonitorUtil.merge2PlanId(date);
		
		
		
		
		return result;
	}
	
	
}
